package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbUserMapper;
import com.dongdongshop.mapper.TbUserScoreAccountMapper;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.pojo.TbUserExample;
import com.dongdongshop.pojo.TbUserScoreAccount;
import com.dongdongshop.pojo.TbUserScoreAccountExample;
import com.dongdongshop.service.TbUserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.bouncycastle.asn1.pkcs.MacData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Date;
import java.util.List;

@Service
@DubboService
public class TbUserServiceImpl implements TbUserService {


    @Autowired
    private TbUserMapper userMapper;
    @Autowired
    private TbUserScoreAccountMapper scoreAccountMapper;
    @Override
    public TbUser selectByUsername(String username) {
        TbUserExample example = new TbUserExample();
        TbUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        List<TbUser> tbUsers = userMapper.selectByExample(example);
        return tbUsers.size() > 0 ? tbUsers.get(0) : null;
    }

    @Override
    public void addUser(TbUser user) {
        userMapper.insertSelective(user);
    }

    @Override
    public void orderScore(Long id,String s) {
        double d = new Double(s);
        Long l = Long.valueOf((long) d);
        TbUserScoreAccountExample example = new TbUserScoreAccountExample();
        TbUserScoreAccountExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(id);
        List<TbUserScoreAccount> tbUserScoreAccounts = scoreAccountMapper.selectByExample(example);
        int size = tbUserScoreAccounts.size();
        if(size > 0){
            TbUserScoreAccount account = tbUserScoreAccounts.get(0);
            account.setUserId(id);
            account.setNum(account.getNum() + l);
            account.setUpdateDate(new Date());
            scoreAccountMapper.updateByPrimaryKeySelective(account);
        }else{
            TbUserScoreAccount account = new TbUserScoreAccount();
            account.setUserId(id);
            account.setNum(l);
            account.setNewDate(new Date());
            account.setUpdateDate(new Date());
            scoreAccountMapper.insertSelective(account);
        }
    }

    @Override
    public void updateUser(TbUser user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public List<TbUser> selectUserListIsSeven() {
        Date min = minSevenData();
        Date max = maxSevenData();
        TbUserExample example = new TbUserExample();
        TbUserExample.Criteria criteria = example.createCriteria();
        criteria.andCreatedLessThan(max);
        criteria.andCreatedGreaterThan(min);
        List<TbUser> tbUsers = userMapper.selectByExample(example);
        return tbUsers;
    }

    private Date minSevenData(){
        Date date = new Date();
        long time = date.getTime();
        Long i = Long.valueOf(1000 * 60 * 60 * 24 * 7);
        time -= i;
        //date转LocalDateTime
        LocalDateTime localDateTime = new Date(time).toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
        //最小时间
        LocalDateTime minTime = localDateTime.with(LocalTime.MIN);
        ZoneId zoneId = ZoneId.systemDefault();
        //时区的日期和时间
        ZonedDateTime zonedDateTime = minTime.atZone(zoneId);
        //获取时刻
        Date min = Date.from(zonedDateTime.toInstant());
        return min;
    }
    private Date maxSevenData(){
        Date date = new Date();
        long time = date.getTime();
        Long i = Long.valueOf(1000 * 60 * 60 * 24 * 7);
        time -= i;
        //date转LocalDateTime
        LocalDateTime localDateTime = new Date(time).toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
        //最大时间
        LocalDateTime maxTime = localDateTime.with(LocalTime.MAX);
        ZoneId zoneId = ZoneId.systemDefault();
        //时区的日期和时间
        ZonedDateTime zonedDateTime = maxTime.atZone(zoneId);
        //获取时刻
        Date max = Date.from(zonedDateTime.toInstant());
        return max;
    }

    public static void main(String[] args) {
        Date date = new Date();
        long time = date.getTime();
        Long i = Long.valueOf(1000 * 60 * 60 * 24 * 7);
        time -= i;
        //date转LocalDateTime
        LocalDateTime localDateTime = new Date(time).toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
        //最小时间
        LocalDateTime minTime = localDateTime.with(LocalTime.MIN);
        LocalDateTime max = localDateTime.with(LocalTime.MAX);
        ZoneId zoneId = ZoneId.systemDefault();
        //时区的日期和时间
        ZonedDateTime zonedDateTime = minTime.atZone(zoneId);
        //获取时刻
        Date min = Date.from(zonedDateTime.toInstant());
        System.out.println(min);
        System.out.println(max);
    }
}
